package com.ysxsoft.wdg.util;

import android.app.Activity;
import android.content.Context;

import com.ali.auth.third.core.MemberSDK;
import com.ali.auth.third.core.callback.LoginCallback;
import com.ali.auth.third.core.model.Session;
import com.ali.auth.third.login.LoginService;
import com.alibaba.baichuan.android.trade.AlibcTrade;
import com.alibaba.baichuan.android.trade.constants.AlibcConstants;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.alibaba.baichuan.android.trade.page.AlibcBasePage;
import com.alibaba.baichuan.android.trade.page.AlibcMyCartsPage;
import com.alibaba.baichuan.android.trade.page.AlibcMyOrdersPage;
import com.alibaba.baichuan.android.trade.page.AlibcPage;

import com.ysxsoft.wdg.bean.tb.TbBean2;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class AlibcUtils {

    /**
     * 登录
     */
    public static void go2login(Context context) {
        LoginService service = MemberSDK.getService(LoginService.class);
        service.setLoginCallback(new LoginCallback() {
            @Override
            public void onSuccess(Session session) {
                ToastUtils.showToast("授权成功");
            }

            @Override
            public void onFailure(int i, String s) {
                ToastUtils.showToast("授权失败"+i+"----"+s);
            }
        });
    }

    /**
     * 显示商品详情页
     */
    public static void showDetail(final Context context,String item_id,String uid) {
            OkHttpUtils.post().url(UrlHost.CONVERT_TBK_URL).tag(context).addParams("uid", ""+uid).addParams("item_id",item_id).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(String response, int id) {
                    TbBean2 tbBean2 = JsonUtils.parseByGson(response, TbBean2.class);
                    if (tbBean2.getCode().equals(CallbackCode.SUCCESS)) {
                        TbBean2.DataBean data = tbBean2.getData();
                        show1(data.getCoupon_click_url(), (Activity) context);
                    } else {
                        ToastUtils.showToast(tbBean2.getMs());
                    }
                }
            });
    }

    /**
     * 显示商品详情页
     */
    public static void showDetail(final Context context,String item_id) {
        String uid = SharePrefUtils.getUserId();
        if (uid.equals("")){
            ToastUtils.showToast("请先登录");
        }else {
            OkHttpUtils.post().url(UrlHost.CONVERT_TBK_URL).tag(context).addParams("uid", ""+uid).addParams("item_id",item_id).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(String response, int id) {
                    TbBean2 tbBean2 = JsonUtils.parseByGson(response, TbBean2.class);
                    if (tbBean2.getCode().equals(CallbackCode.SUCCESS)) {
                        TbBean2.DataBean data = tbBean2.getData();
                        show1(data.getCoupon_click_url(), (Activity) context);
                    } else {
                        ToastUtils.showToast(tbBean2.getMs());
                    }
                }
            });
//            OkGo.<String>post(Urls.CONVERT_TBK_URL)
//                    .tag(context)
//                    .params("uid", uid)
//                    .params("item_id", item_id)
//                    .execute(new StringCallback() {
//                        @Override
//                        public void onSuccess(Response<String> response) {
//                            KLog.e(response.body().toString());
//
//                        }
//                    });
        }
    }

    public static void show1(String click_url, Activity context) {
        //提供给三方传递配置参数
        Map<String, String> exParams = new HashMap<>();
        // 固定写法
        exParams.put(AlibcConstants.ISV_CODE, "appisvcode");
        exParams.put("alibaba", "阿里巴巴");//自定义参数部分，可任意增删改
        //商品详情page
        AlibcBasePage detailPage = new AlibcPage(click_url);
        //使用百川sdk提供默认的Activity打开detail
        AlibcTrade.show(context, detailPage,
                new AlibcShowParams(OpenType.Auto, false),
                null, exParams,
                new TaoBaoTradeCallback());
    }


    /**
     * 购物车
     */
    public static void showCart(Context context) {
        //提供给三方传递配置参数
        Map<String, String> exParams = new HashMap<>();
        // 固定写法
        exParams.put(AlibcConstants.ISV_CODE, "appisvcode");
        exParams.put("alibaba", "阿里巴巴");//自定义参数部分，可任意增删改
        //商品详情page
        AlibcBasePage myCartsPage = new AlibcMyCartsPage();
        //使用百川sdk提供默认的Activity打开detail
        AlibcTrade.show((Activity) context, myCartsPage,
                new AlibcShowParams(OpenType.Auto, false),
                null, exParams,
                new TaoBaoTradeCallback());

    }

    /**
     * 我的订单
     */
    public static void showOrder(Context context) {
        //提供给三方传递配置参数
        Map<String, String> exParams = new HashMap<>();
        // 固定写法
        exParams.put(AlibcConstants.ISV_CODE, "appisvcode");
        exParams.put("alibaba", "阿里巴巴");//自定义参数部分，可任意增删改
        //商品详情page
        AlibcBasePage myOrdersPage = new AlibcMyOrdersPage(0,true);//订单页面参数，仅在H5方式下有效
        //使用百川sdk提供默认的Activity打开detail
        AlibcTrade.show((Activity) context, myOrdersPage, new AlibcShowParams(OpenType.Auto, false),
                null, exParams,
                new  TaoBaoTradeCallback());
    }
}
